package com.lostreseditores.sabersai.presentation.backingBeans;

import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

import javax.faces.model.SelectItem;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.component.selectonemenu.SelectOneMenu;
import org.primefaces.event.RowEditEvent;

import com.lostreseditores.sabersai.exceptions.ZMessManager;
import com.lostreseditores.sabersai.model.Distribuidor;
import com.lostreseditores.sabersai.model.dto.DistribuidorDTO;
import com.lostreseditores.sabersai.presentation.businessDelegate.BusinessDelegatorView;
import com.lostreseditores.sabersai.utilities.FacesUtils;


public class DistribuidorView {
    private InputText txtDistcorreov;
    private InputText txtDistmovilv;
    private InputText txtDistnombrev;
    private InputText txtDisttelefonov;
    private InputText txtDistidn;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<DistribuidorDTO> data;
    private DistribuidorDTO selectedDistribuidor;
    private SelectOneMenu cmbDistribuidor;
    private String distribuidorSelect;
    private List<SelectItem> list_Distribuidor = new ArrayList<SelectItem>();

    public DistribuidorView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            DistribuidorDTO distribuidorDTO = (DistribuidorDTO) e.getObject();

            if (txtDistcorreov == null) {
                txtDistcorreov = new InputText();
            }

            txtDistcorreov.setValue(distribuidorDTO.getDistcorreov());

            if (txtDistmovilv == null) {
                txtDistmovilv = new InputText();
            }

            txtDistmovilv.setValue(distribuidorDTO.getDistmovilv());

            if (txtDistnombrev == null) {
                txtDistnombrev = new InputText();
            }

            txtDistnombrev.setValue(distribuidorDTO.getDistnombrev());

            if (txtDisttelefonov == null) {
                txtDisttelefonov = new InputText();
            }

            txtDisttelefonov.setValue(distribuidorDTO.getDisttelefonov());

            if (txtDistidn == null) {
                txtDistidn = new InputText();
            }

            txtDistidn.setValue(distribuidorDTO.getDistidn());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtDistcorreov != null) {
            txtDistcorreov.setValue(null);
            txtDistcorreov.setDisabled(true);
        }

        if (txtDistmovilv != null) {
            txtDistmovilv.setValue(null);
            txtDistmovilv.setDisabled(true);
        }

        if (txtDistnombrev != null) {
            txtDistnombrev.setValue(null);
            txtDistnombrev.setDisabled(true);
        }

        if (txtDisttelefonov != null) {
            txtDisttelefonov.setValue(null);
            txtDisttelefonov.setDisabled(true);
        }

        if (txtDistidn != null) {
            txtDistidn.setValue(null);
            txtDistidn.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Distribuidor entity = null;

        try {
            Integer distidn = new Integer(txtDistidn.getValue().toString());
            entity = BusinessDelegatorView.getDistribuidor(distidn);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtDistcorreov.setDisabled(false);
            txtDistmovilv.setDisabled(false);
            txtDistnombrev.setDisabled(false);
            txtDisttelefonov.setDisabled(false);
            txtDistidn.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtDistcorreov.setValue(entity.getDistcorreov());
            txtDistcorreov.setDisabled(false);
            txtDistmovilv.setValue(entity.getDistmovilv());
            txtDistmovilv.setDisabled(false);
            txtDistnombrev.setValue(entity.getDistnombrev());
            txtDistnombrev.setDisabled(false);
            txtDisttelefonov.setValue(entity.getDisttelefonov());
            txtDisttelefonov.setDisabled(false);
            txtDistidn.setValue(entity.getDistidn());
            txtDistidn.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveDistribuidor(FacesUtils.checkString(
                    txtDistcorreov), FacesUtils.checkInteger(txtDistidn),
                FacesUtils.checkString(txtDistmovilv),
                FacesUtils.checkString(txtDistnombrev),
                FacesUtils.checkString(txtDisttelefonov));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteDistribuidor(FacesUtils.checkInteger(
                    txtDistidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateDistribuidor(FacesUtils.checkString(
                    txtDistcorreov), FacesUtils.checkInteger(txtDistidn),
                FacesUtils.checkString(txtDistmovilv),
                FacesUtils.checkString(txtDistnombrev),
                FacesUtils.checkString(txtDisttelefonov));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtDistidn == null) {
                txtDistidn = new InputText();
            }

            txtDistidn.setValue(selectedDistribuidor.getDistidn());

            BusinessDelegatorView.deleteDistribuidor(FacesUtils.checkInteger(
                    txtDistidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedDistribuidor);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(String distcorreov, Integer distidn,
        String distmovilv, String distnombrev, String disttelefonov)
        throws Exception {
        try {
            BusinessDelegatorView.updateDistribuidor(distcorreov, distidn,
                distmovilv, distnombrev, disttelefonov);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("DistribuidorView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtDistcorreov() {
        return txtDistcorreov;
    }

    public void setTxtDistcorreov(InputText txtDistcorreov) {
        this.txtDistcorreov = txtDistcorreov;
    }

    public InputText getTxtDistmovilv() {
        return txtDistmovilv;
    }

    public void setTxtDistmovilv(InputText txtDistmovilv) {
        this.txtDistmovilv = txtDistmovilv;
    }

    public InputText getTxtDistnombrev() {
        return txtDistnombrev;
    }

    public void setTxtDistnombrev(InputText txtDistnombrev) {
        this.txtDistnombrev = txtDistnombrev;
    }

    public InputText getTxtDisttelefonov() {
        return txtDisttelefonov;
    }

    public void setTxtDisttelefonov(InputText txtDisttelefonov) {
        this.txtDisttelefonov = txtDisttelefonov;
    }

    public InputText getTxtDistidn() {
        return txtDistidn;
    }

    public void setTxtDistidn(InputText txtDistidn) {
        this.txtDistidn = txtDistidn;
    }

    public List<DistribuidorDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataDistribuidor();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<DistribuidorDTO> distribuidorDTO) {
        this.data = distribuidorDTO;
    }

    public DistribuidorDTO getSelectedDistribuidor() {
        return selectedDistribuidor;
    }

    public void setSelectedDistribuidor(DistribuidorDTO distribuidor) {
        this.selectedDistribuidor = distribuidor;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }

	public SelectOneMenu getCmbDistribuidor() {
		try {
			list_Distribuidor.clear();
			List<Distribuidor> distribuidor = BusinessDelegatorView
					.getDistribuidor();
			SelectItem item = new SelectItem(0,
					"Seleccione Uno");
			list_Distribuidor.add(item);
			for (Distribuidor d : distribuidor) {
				item = new SelectItem(d.getDistnombrev());
				list_Distribuidor.add(item);
			}
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}	
		return cmbDistribuidor;
	
	}
	

	public void setCmbDistribuidor(SelectOneMenu cmbDistribuidor) {
		this.cmbDistribuidor = cmbDistribuidor;
	}

	public String getDistribuidorSelect() {
		return distribuidorSelect;
	}

	public void setDistribuidorSelect(String distribuidorSelect) {
		this.distribuidorSelect = distribuidorSelect;
	}

	public List<SelectItem> getList_Distribuidor() {
		return list_Distribuidor;
	}

	public void setList_Distribuidor(List<SelectItem> list_Distribuidor) {
		this.list_Distribuidor = list_Distribuidor;
	}
    
    
}
